package cn.wolfcode.luowowo.hotel.service.impl;

import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.hotel.domain.HotelOrder;
import cn.wolfcode.luowowo.hotel.domain.HotelRoomType;
import cn.wolfcode.luowowo.hotel.mapper.HotelOrderMapper;
import cn.wolfcode.luowowo.hotel.mapper.HotelRoomTypeMapper;
import cn.wolfcode.luowowo.hotel.query.HotelOrderQueryObject;
import cn.wolfcode.luowowo.hotel.query.HotelQueryObject;
import cn.wolfcode.luowowo.hotel.service.IHotelOrderService;
import cn.wolfcode.luowowo.hotel.service.IHotelService;
import cn.wolfcode.luowowo.hotel.vo.HotelOrderVo;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 酒店主题
 *
 * @author win
 */
@Service
public class HotelOrderServiceImpl implements IHotelOrderService {

    @Autowired
    private HotelOrderMapper hotelOrderMapper;

    @Autowired
    private IHotelService hotelService;

    @Autowired
    private HotelRoomTypeMapper hotelRoomTypeMapper;

    @Override
    public void deleteById(Long id) {
        hotelOrderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(HotelOrder theme) {
        if (theme == null) {
            return;
        }
        if (theme.getId() == null) {
            hotelOrderMapper.insert(theme);
        } else {
            hotelOrderMapper.updateByPrimaryKey(theme);
        }
        // TODO 后续维护关系
    }

    @Override
    public HotelOrder findById(Long id) {
        return hotelOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<HotelOrder> selectAll() {
        return hotelOrderMapper.selectAll();
    }

    @Override
    public PageInfo<HotelOrder> query(HotelOrderQueryObject qo) {
        return null;
    }

    @Override
    public boolean orderHotel(HotelOrderVo vo, UserInfo userInfo) {
        // 判断是否有房间
        HotelQueryObject queryObject = new HotelQueryObject();
        BeanUtils.copyProperties(vo, queryObject);
        Map<String, Object> map = hotelService.thereRoom(queryObject);
        if (!map.containsKey(String.valueOf(vo.getType()))) {
            throw new LogicException("参数异常");
        }
        // 获取指定类型的房间的数量
        int quantity = (int) map.get(String.valueOf(vo.getType()));
        if (quantity == 0 || quantity < vo.getRooms()) {
            // 没有房间
            return false;
        }
        // 计算价格
        // 查出该房间类型的价格
        HotelRoomType hotelRoomType = hotelRoomTypeMapper.findByHotelIdAndType(vo.getHotelId(), vo.getType());
        if (hotelRoomType == null) {
            throw new LogicException("参数错误");
        }
        vo.setUserId(userInfo.getId());
        /**
         * 总价格
         */
        long price = hotelRoomType.getPrice() * vo.getRooms();
        vo.setPrice(price);
        // 如果有房间就订购
        // 生成订单
        hotelOrderMapper.addOrder(vo);
        return true;
    }

    @Override
    public List<HotelOrder> queryUserHotelOrderByUserId(Long userId) {
        if (userId == null) {
            throw new LogicException("请先登录");
        }
        return hotelOrderMapper.selectUserHotelOrderByUserId(userId);
    }
}
